﻿package {
	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;
	
	public class Human extends Sprite {
		
		public var humanSprite:MovieClip;
		private var defaultScale = 0.5;
		private var score = 1;
		public var stepX = 1;
		private var myGotLoot = false;
		private var lootCounter = 50;
		private var imLooting = false;
		
		public function Human()
		{
			var rand = Math.random();
			if (rand < 0.1) {
				humanSprite = new TVHumanSprite();
				score = 3;
			} else {
				humanSprite = new HumanSprite();
			}
			addChild(humanSprite);
			
			this.scaleX = this.scaleY = defaultScale + ((Math.random() * 0.1) - 0.05);
		}
		
		public function step()
		{
			if (imLooting)
			{
				loot();
			}
			else
			{
				moveMe();
			}
		}
		
		public function checkZombies(zombieHorde)
		{
			if (zombieHorde == null)
			{
				return;
			}
			
			for (var i = 0; i < zombieHorde.length; i++)
			{
				var z = zombieHorde[i];
				var zTop = z.y - (z.height / 2);
				var zBottom = z.y + (z.height / 2);

				var myTop = this.y - (this.height / 2);
				var myBottom = this.y + (this.height / 2);
				
				if ((zBottom > myTop && zBottom < myBottom) || (zTop > myTop && zTop < myBottom) || z.y == this.y)
				{
//					trace("zombie ahead!");
					if (z.y < this.y)
					{
						this.y += 2;
					}
					else 
					{
						this.y -= 2;
					}
					break;
				}
			}
		}
		
		public function moveMe()
		{
			if (!imLooting)
			{
				x += (10 * stepX); 
			}
			//trace("moving human X to :"+x);//TMP
			
			var room = (parent as Room);
			if (this.y >= room.rectangle.height)
			{
				this.y = room.rectangle.height;
			}
			else if (this.y < 0)
			{
				this.y = 0;
			}
		}
		
		public function set gotLoot(newLootValue)
		{
			myGotLoot = newLootValue;
			stepX = -stepX;
			//trace(stepX);
			if (newLootValue) {
				isLooting = false;
				addChild(humanSprite);
				humanSprite.gotoAndPlay(0);
			} else {		
				humanSprite.gotoAndPlay(9);
			}
		}
		
		public function get gotLoot()
		{
			return myGotLoot;
		}
		
		public function getScore()
		{
			return score;
		}
		
		public function set isLooting(lootingVal)
		{
			imLooting = lootingVal;
			
			if(imLooting)
			{
				removeChild(humanSprite);
			}
			else
			{
				addChild(humanSprite);
			}
		}
		
		public function get isLooting()
		{
			return imLooting
		}
			
		
		public function loot()
		{
			if (lootCounter == 0)
			{
				gotLoot = true;
			}
			
			lootCounter--;
		}
	}
	
}